package Controls
{
	import Config.GameConfig;
	
	import Load.DataFormat;
	import Load.LoadData;
	import Load.MixedLoad;
	
	import Objects.GameBitmap;
	
	import Tools.BitmapTool;
	
	import flash.display.Bitmap;
	import flash.utils.Dictionary;

	public class BitmapMovie extends GameBitmap
	{
		public var frames:Array=[];
		private var animFrame:Number=0;
		public var freeze:Boolean=false;
		public var animSpeed:int=0;
		private var startIndex:int=0;
		private var endIndex:int=-1;
		private var reverse:Boolean=false;
		private var labels:Dictionary;
		private var lastLabel:String;
		public function BitmapMovie()
		{
			super();
			animSpeed=GameConfig.animSpeed;
			labels=new Dictionary();
			intelligent=true;
		}
		override protected function $update():void
		{
			syncMovie();
		}
		private function syncMovie():void
		{
			if (frames.length>0)
			{
				if (animSpeed > 0&&!freeze)
				{
					animFrame=animFrame+1/animSpeed;
					setImage(frames[movieFrame()]);
				}
			}
		}
		public function reversePlay():void
		{
			if(reverse)return;
			animFrame=0;
			reverse=true;
		}
		public function setup(imageData:Object,columns:int,rows:int):void
		{
			frames=BitmapTool.cut(imageData,columns,rows);
		}
		public function load(url:String,columns:int,rows:int):void
		{
			var loadData:LoadData=new LoadData();
			loadData.url=url;
			loadData.addCarry("columns",columns);
			loadData.addCarry("rows",rows);
			loadData.dataFormat=DataFormat.IMAGE;
			MixedLoad.load([loadData],loadFinish);
		}
		private function loadFinish(loadData:LoadData):void
		{
			var bitmap:Bitmap=loadData.data as Bitmap;
			if(bitmap)
			{
				setup(bitmap.bitmapData,int(loadData.getCarry("columns")),int(loadData.getCarry("rows")));
			}
		}
		private function movieFrame():int
		{
			var frame:int=0;
			var cycle:int=movieCycle();
			frame=Math.floor(animFrame)%cycle;
			if(reverse)
			{
				frame=cycle-frame-1;
			}
			frame=startIndex+frame;
			return frame;
		}
		private function movieCycle():int
		{
			var value:int=0;
			if(endIndex>=0)
			{
				value=endIndex-startIndex+1;
			}
			else
			{
				value=frames.length;
			}
			return value;
		}
		public function addLabel(name:String,start:int,end:int):void
		{
			var labelData:Object=new Object();
			labelData["name"]=name;
			labelData["start"]=start;
			labelData["end"]=end;
			labels[name]=labelData;
		}
		public function playLabel(label:String):void
		{
			if(lastLabel==label)return;
			lastLabel=label;
			animFrame=0;
			var labelData:Object=labels[label];
			startIndex=labelData["start"]-1;
			endIndex=labelData["end"]-1;
		}
		public function get movieEnd():Boolean
		{
			var cycle:int=movieCycle();
			var frame:int=movieFrame();
			if(reverse)
			{
				return frame==startIndex;
			}
			else
			{
				return frame==startIndex+cycle-1;
			}
		}
		public function get movieStart():Boolean
		{
			var cycle:int=movieCycle();
			var frame:int=movieFrame();
			if(reverse)
			{
				return frame==startIndex+cycle-1;
			}
			else
			{
				return frame==startIndex;
			}
		}
	}
}